#!/bin/python

# --- xxxxx (sql_file -> new_table -> new_database -> row) xxxx
# --- (GSP_PURCHASE_CHANGE -> od_purchase_change -> gsp_order -> 1000)
# select * from GSP_PURCHASE_CHANGE where modify_time > '2020-01-01';

file = open(r"reque", "r")
data = file.read().split('\n')
file.close()

sql_file = ''
t_table = ''
t_db = ''
sql = ''
row = ''

default_row = '5000'

for line in data:
    line = line.lstrip()
    i = line.find("(")
    j = line.find("->")
    k = line.find(")")

    if line[0:3] == '---' and j > 0:
        if sql != '' and sql_file != '':
            f = open(sql_file + '.txt', 'w')
            f.write(sql.split(';')[0])
            f.close
            print(sql_file + ' -> ' + t_table + ' -> ' + t_db + ' -> ' + row)
        tmp = line[i+1:k].split('->')

        sql_file = tmp[0].strip()
        t_table = tmp[1].strip()
        t_db = tmp[2].strip()
        row = default_row
        if len(tmp) > 3:
            row = tmp[3].strip()
        sql = ''
    else:
        sql = sql + '\n' + line
if sql != '' and sql_file != '':
    f = open(sql_file + '.txt', 'w')
    f.write(sql.split(';')[0])
    f.close
    print(sql_file + ' -> ' + t_table + ' -> ' + t_db + ' -> ' + row)
